package dto

import (
	"github.com/gin-gonic/gin"
)

const (
	Success       = 0
	Error         = 500   //服务内部错误
	ErrBadRequest = 20400 //请求参数错误
)

var msgCode = map[int]string{
	Success:       "ok",
	Error:         "服务内部错误",
	ErrBadRequest: "请求参数错误",
}

// GetMsg get msg by code
func GetMsg(code int) string {
	var (
		msg    string
		exists bool
	)

	if msg, exists = msgCode[code]; exists {
		return msg
	}
	return "unknown"
}

// Response api response
type Response struct {
	Code  int         `json:"code" comment:"111"` // msg
	Msg   string      `json:"msg"`                // code
	Data  interface{} `json:"data" form:"111"`    // data
	Count int         `json:"count,omitempty"`    // data count
}

// JSON gin resp to json
func JSON(c *gin.Context, code int, data ...interface{}) {
	resp := Response{
		Code: code,
		Msg:  GetMsg(code),
		Data: data[0],
	}
	if len(data) == 2 {
		resp.Count = data[1].(int)
	}
	c.JSON(200, resp)
	c.Set("statuscode", code)
	return
}

type QueryStockResp struct {
	Records []StockResp `json:"record"`
	Total   int         `json:"total"`
}

type StockResp struct {
	Symbol             string      `json:"symbol"` //编号
	Name               string      `json:"name"`   //名称
	Type               int         `json:"type"`
	Percent            float64     `json:"percent"`            //当日涨跌
	Current            float64     `json:"current"`            //最新价格
	Amplitude          float64     `json:"amplitude"`          //当日振幅
	CurrentYearPercent float64     `json:"currentYearPercent"` //年初至今涨幅
	MarketCapitalFloat float64     `json:"marketCapital"`      //总市值
	DividendYield      float64     `json:"dividendYield"`      //股息率
	Chg                float64     `json:"chg"`                //当日涨跌额
	Eps                float64     `json:"eps"`                //每股收益
	Volume             int         `json:"volume"`             //成交量
	VolumeRatio        float64     `json:"volumeRatio"`        //量比
	Pb                 float64     `json:"pb"`                 //市净率
	TurnoverRate       float64     `json:"turnoverRate"`       //换手率
	PeTtm              float64     `json:"peTtm"`              //市盈率
	ReadPrice          interface{} `json:"readPrice"`          //关注价格
	ReadStatus         string      `json:"readStatus"`         //阅读状态 1.重点关注；2.稍微关注；0.已阅不关注
	ReadTimeStr        string      `json:"readTime"`           //关注时间
	Comment            string      `json:"comment"`            //评论
	ReadPercent        interface{} `json:"readPercent"`        //关注至今幅度
	TagArray           []string    `json:"tags"`               //计算出的标签
}

type TagResp struct {
	Name  string `json:"name"`  //名称
	Value string `json:"value"` //值
}

//爬虫stock-resp
type XueQiuAllDataResp struct {
	Data struct {
		Count int `json:"count"`
		List  []struct {
			Symbol             string  `json:"symbol"`
			NetProfitCagr      float64 `json:"net_profit_cagr"`
			NorthNetInflow     float64 `json:"north_net_inflow"`
			Ps                 float64 `json:"ps"`
			Type               int     `json:"type"`
			Percent            float64 `json:"percent"`
			HasFollow          bool    `json:"has_follow"`
			TickSize           float64 `json:"tick_size"`
			PbTtm              float64 `json:"pb_ttm"`
			FloatShares        int     `json:"float_shares"`
			Current            float64 `json:"current"`
			Amplitude          float64 `json:"amplitude"`
			Pcf                float64 `json:"pcf"`
			CurrentYearPercent float64 `json:"current_year_percent"`
			FloatMarketCapital int64   `json:"float_market_capital"`
			NorthNetInflowTime int64   `json:"north_net_inflow_time"`
			MarketCapital      int64   `json:"market_capital"`
			DividendYield      float64 `json:"dividend_yield"`
			LotSize            int     `json:"lot_size"`
			RoeTtm             float64 `json:"roe_ttm"`
			TotalPercent       float64 `json:"total_percent"`
			Percent5M          float64 `json:"percent5m"`
			IncomeCagr         float64 `json:"income_cagr"`
			Amount             float64 `json:"amount"`
			Chg                float64 `json:"chg"`
			IssueDateTs        int64   `json:"issue_date_ts"`
			Eps                float64 `json:"eps"`
			MainNetInflows     float64 `json:"main_net_inflows"`
			Volume             int     `json:"volume"`
			VolumeRatio        float64 `json:"volume_ratio"`
			Pb                 float64 `json:"pb"`
			Followers          int     `json:"followers"`
			TurnoverRate       float64 `json:"turnover_rate"`
			FirstPercent       float64 `json:"first_percent"`
			Name               string  `json:"name"`
			PeTtm              float64 `json:"pe_ttm"`
			TotalShares        int     `json:"total_shares"`
			LimitupDays        int     `json:"limitup_days"`
		} `json:"list"`
	} `json:"data"`
}

//爬虫kline-resp
type KlineDataResp struct {
	Data struct {
		Symbol string      `json:"symbol"`
		Column []string    `json:"column"`
		Item   [][]float64 `json:"item"`
	} `json:"data"`
	ErrorCode        int    `json:"error_code"`
	ErrorDescription string `json:"error_description"`
}
